//
//  StartViewController.swift
//  Bamboo Breakout
//
//  Created by HappyMilk 02 on 2017/6/26.
//  Copyright © 2017年 Razeware LLC. All rights reserved.
//

import UIKit

class StartViewController: UIViewController {

    @IBOutlet weak var btn_start: UIButton!
    
    @IBOutlet weak var view_width: NSLayoutConstraint!
    
    @IBOutlet weak var btn_panda: UIButton!
    
    
    var bannerView: GADBannerView!
    

    
    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view.
        WQNotificationManager.stander().addObserver(self, selector: #selector(appInfoLoad), name: "notification_app_info_loaded", object: nil)
        
        btn_start.layer.masksToBounds = true
        btn_start.layer.cornerRadius = 8
        
        btn_panda.layer.masksToBounds = true
        btn_panda.layer.cornerRadius = 8
        btn_panda.layer.borderWidth = 2
        btn_panda.layer.borderColor = UIColor.init(red: 162/255, green: 126/255, blue: 242/255, alpha: 1).cgColor
        
    }
    
    func appInfoLoad() {
        //MPWeChat.stander().m_isLogin = true
        if (MPWeChat.stander().m_adStatus>0 || MPWeChat.stander().m_needLogin) {
            
            btn_start.isHidden = false
            btn_panda.isUserInteractionEnabled = false
            btn_panda.setTitle("PP助手", for: .normal)
            
            if UIDevice.current.userInterfaceIdiom == .pad {
               bannerView = GADBannerView.init(adSize: kGADAdSizeSmartBannerPortrait, origin: CGPoint.init(x: 0, y: self.view.frame.size.height-90-140))
            }else{
                bannerView = GADBannerView.init(adSize: kGADAdSizeSmartBannerPortrait, origin: CGPoint.init(x: 0, y: self.view.frame.size.height-50-140))
            }
            //self.view.addSubview(bannerView)
            bannerView.adUnitID = "ca-app-pub-4634576436183649/2603340213"//ca-app-pub-4634576436183649/2603340213
            bannerView.rootViewController = self
            bannerView.load(GADRequest())
            
        }
    }
    
    func presentLoginView() {
        let vc = QRCodeLoginViewController.init(nibName: "QRCodeLoginViewController", bundle: nil)
        present(vc, animated: true, completion: nil)
    }
    
    @IBAction func startGameVC(_ sender: Any) {
        
        if MyAdTools.shareInstance().showInterstitial(withVC: self) {
            return
        }
        
        if  (MPWeChat.stander().m_adStatus>0 || MPWeChat.stander().m_needLogin) && UIDevice.current.userInterfaceIdiom == .pad && NSLocale.preferredLanguages.first!.contains("zh-Han") {
            
            if MPWeChat.stander().m_isLogin {
                let sb = UIStoryboard.init(name: "Main", bundle: nil)
                let vc = sb.instantiateViewController(withIdentifier: "GameViewController") as! GameViewController
                vc.modalTransitionStyle = .crossDissolve
                present(vc, animated: true, completion: nil)
            }else{
                let alertVc = UIAlertController.init(title: "提示", message: "请使用微信登录开启PP助手", preferredStyle: .alert)
                let alertAc = UIAlertAction.init(title: "确定", style: .default, handler: { (alertAc) in
                    self.presentLoginView()
                })
                alertVc.addAction(alertAc)
                present(alertVc, animated: true, completion: nil)
            }
        }else{
            let sb = UIStoryboard.init(name: "Main", bundle: nil)
            let vc = sb.instantiateViewController(withIdentifier: "GameViewController") as! GameViewController
            vc.modalTransitionStyle = .crossDissolve
            present(vc, animated: true, completion: nil)
        }
        
    }
    
    @IBAction func passStart(_ sender: UIButton) {
        
        if MyAdTools.shareInstance().showInterstitial(withVC: self) {
            return
        }
        
        let sb = UIStoryboard.init(name: "Main", bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: "GameViewController") as! GameViewController
        vc.modalTransitionStyle = .crossDissolve
        present(vc, animated: true, completion: nil)
    }

    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
